<template>
  <el-row type="flex" justify="center">
    <el-col :span="8">
      <el-form :model="pwdForm" :rules="rules" ref="pwdForm" label-width="100px">
        <el-form-item label="旧密码" prop="oldPwd">
          <el-input v-model="pwdForm.oldPwd" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPwd">
          <el-input v-model="pwdForm.newPwd" show-password></el-input>
        </el-form-item>    
        <el-form-item>
          <el-button type="primary" plain @click="changPassword">修改密码</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
  
</template>
<script lang="ts">
import {defineComponent} from 'vue'
import {changePwd} from '@/api/user'
import {ElMessage} from 'element-plus'
export default defineComponent({
  name:'ChangePwd',
  setup(){
    const rules = {
      oldPwd: [
        { required: true, message: '请输入旧密码', trigger: 'blur' },
        { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
      ],
      newPwd: [
        { required: true, message: '请输入新密码', trigger: 'blur' },
        { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
      ]
    }
    return {
      rules
    }
  },
  data() {
    return {
      pwdForm: {
        oldPwd: '',
        newPwd: ''
      }
    }
  },
  methods: {
    // 修改密码
    changPassword() {
      changePwd(this.pwdForm).then(res => {
        let rs = res as any
        if(rs.code === 20000) {
          ElMessage.success('密码修改成功')
        }
        else{
          ElMessage.error(rs.message)
        }
      })
    }
  }
})
</script>
<style scoped>
</style>
